<template>
	<view class="pages-box rel" >
		<fixed>
			<!-- #ifndef H5 -->
			<uni-nav-bar :fixed="false" :shadow="false" :statusBar="true" title="员工列表" leftIcon="icon-left"
				color="#ffffff" :backgroundColor="primaryColor">
			</uni-nav-bar>
			<!-- <view :style="{height:`${configInfo.navBarHeight}px`}"></view> -->
			<!-- #endif -->
			<view class="fill-base">
				<search class="flex-1" @input="toSearch" :keyword="param.name" type="input" :padding="20" height="80rpx" :radius="80" backgroundColor="#fff"
					placeholder="搜索员工名字">
				</search> 
			</view>
		</fixed>
		<block v-if="isLoad">
			<view class="pd-lg">
				<view class="fill-base radius-16 coach-item flex-y-center mb-md pd-lg" v-for="(item,index) in list" :key="index">
					<image :src="item.work_img" mode="aspectFill" class="coach-header"></image>
					<view class="flex-1 pr-lg pl-md f-paragraph">{{item.name}}</view>
					<view class="ass-btn radius-10 flex-center c-base f-desc" 
					:style="{background: primaryColor}" @tap="submit(item.id)">确认指派</view>
				</view>
			</view>
			<abnor v-if="list.length <= 0"></abnor>
		</block> 
		
		
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	let timer = null
	export default {
		data() {
			return {
				param: {
					name: '',
					s_type: ''
				},
				options:{},
				isLoad: false,
				loading: true,
				list: []
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo
		}),
		async onLoad(options) { 
			this.$util.showLoading()
			this.options = options
			this.param.s_type = options.s_type
			this.initIndex()
		},
		async onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		methods: {
			...mapActions(['getConfigInfo']),
			...mapMutations(['updatePoolItem', 'updateHomeItem']),
			async initIndex(refresh = false){
				if(!this.configInfo.id || refresh){
					this.getConfigInfo()
				}
				this.getList()
			},
			async initRefresh(){
				this.$util.showLoading()
				this.initIndex(true)
			},
			toSearch(name){
				clearTimeout(timer)
				timer = setTimeout(()=>{
					this.param.name = name
					this.$util.showLoading()
					this.getList(this.param)
				},1000)
			},
			async getList(){
				let {
					list: oldList,
					param
				} = this
				let newList = await this.$api.pool.coachList(param)
				// if (param.page == 1) {
				// 	this.list = newList;
				// } else {
				// 	newList.data = oldList.data.concat(newList.data)
				// 	this.list = newList;
				// }
				this.list = newList
				this.loading = false
				this.isLoad = true
				this.$util.hideAll()
			},
			async submit(id){
				let {
					delta = 1,
					id: orderid = '',
					type = 1
				} = this.options
				this.$util.showLoading()
				let methodModel = {
					1: 'assignCoach',
					2: 'demandAssignCoach'
				}
				let data = await this.$api.pool[methodModel[type]]({order_id: orderid,coach_id: id})
				this.$util.hideAll()
				if( data && data.err_code == 'kezhi'){
					let [res_del, {
						confirm
					}] = await uni.showModal({
						content: data.err_msg,
					})
					if(confirm){
						this.$util.goUrl({url: `/user/pages/wallet/index`})
						return
					}
					if (!confirm) return;
				}
				this.$util.showToast({
					title: '指派成功'
				})
				// setTimeout(()=>{
				// 	this.updateHomeItem({
				// 		key: 'pageActive',
				// 		val: false
				// 	})
				// 	let pages = getCurrentPages(); //当前页面栈 
				// 	if (pages.length > 1) {
				// 		var beforePage = pages[pages.length - (delta*1+1)]; //获取上一个页面实例对象  
				// 		//触发父页面中的方法change()  
				// 		beforePage.$vm.initRefresh()
				// 	}
				// 	this.$util.goUrl({url: delta*1 ,openType: 'navigateBack'})
				// },1500)
			}
		},
	}
</script>


<style lang="scss">
	.pages-box {
		.item-title{
			line-height: 40rpx;
			height: 80rpx;
		}
		.item-image{
			width: 248rpx;
			height: 219rpx;
		}
		.item-num{
			.item-num-bg{
				width: 100%;
				height: 100%;
				left: 0;
				top: 0;
				border-radius: 40rpx;
				opacity: 0.1;
			}
		}
		.coach-header{
			width: 80rpx;
			height: 80rpx;
			border-radius: 80rpx;
		}
		.ass-btn{
			width: 140rpx;
			height: 66rpx;
		}
	}
</style>
